﻿using Newtonsoft.Json;

namespace BaseDll
{
    /// <summary>
    /// 文件数据基类
    /// </summary>
    public class FileDataClass
    {

        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static T Get<T>() where T : class, new()
        {
            var type = typeof(T);
            if (!File.Exists(type.Name))
            {
                T parameter = new T();
                var stram = File.Create(type.Name);
                stram.Close();
                File.WriteAllText(type.Name, Newtonsoft.Json.JsonConvert.SerializeObject(parameter));
                return parameter;
            }
            var value = File.ReadAllText(type.Name);
            return JsonConvert.DeserializeObject<T>(value);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameter"></param>
        public static void Save<T>(T parameter)
        {
            var type = typeof(T);
            if (File.Exists(type.Name))
            {
                File.Delete(type.Name);
            }
            var stram = File.Create(type.Name);
            stram.Close();
            File.WriteAllText(type.Name, Newtonsoft.Json.JsonConvert.SerializeObject(parameter));
        }
    }
}
